# 导入pygame库
import pygame
import random

# 初始化pygame
pygame.init()

# 设置游戏窗口大小
screen = pygame.display.set_mode((480, 700))

# 设置游戏窗口标题
pygame.display.set_caption("飞机大战")

# 加载背景图片
background = pygame.image.load("images/background.png")

# 让背景图片拉伸显示
background = pygame.transform.scale(background, (480, 700))

# 加载飞机图片
player = pygame.image.load("images/player.png")

# 加载敌机图片
enemy = pygame.image.load("images/enemy.png")

# 加载子弹图片
bullet = pygame.image.load("images/bullet.png")

# 加载爆炸特效图片
explosion = pygame.image.load("images/explosion.png")

# 设置游戏时钟
clock = pygame.time.Clock()

# 设置飞机的初始位置
player_x = 200
player_y = 500

# 设置飞机的移动速度
player_speed = 5

# 设置敌机的初始位置
enemy_x = 200
enemy_y = 0

# 设置敌机的移动速度
enemy_speed = 3

# 设置子弹的初始位置
bullet_x = 0
bullet_y = 0

# 设置子弹的移动速度
bullet_speed = 10

# 设置自动发射子弹的时间间隔
bullet_interval = 1

# 生成敌机列表
enemies = []

# 生成爆炸特效列表
explosions = []

# 游戏循环
while True:
    # 设置游戏帧率
    clock.tick(60)

    # 处理事件
    for event in pygame.event.get():
        # 如果是退出事件，则退出游戏
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()

    # 获取键盘按键状态
    keys = pygame.key.get_pressed()

    # 处理键盘按键事件
    if keys[pygame.K_LEFT]:
        player_x -= player_speed
    if keys[pygame.K_RIGHT]:
        player_x += player_speed
    if keys[pygame.K_UP]:
        player_y -= player_speed
    if keys[pygame.K_DOWN]:
        player_y += player_speed

    # 自动发射子弹
    bullet_interval += 1
    if bullet_interval >= 10:
        bullet_x = player_x + player.get_width() / 2 - bullet.get_width() / 2
        bullet_y = player_y - bullet.get_height()
        bullet_interval = 1

    # 生成敌机
    if len(enemies) < 5:
        enemy_x = random.randint(0, 480 - enemy.get_width())
        enemy_y = random.randint(-500, -enemy.get_height())
        enemies.append([enemy_x, enemy_y])

    # 移动敌机
    for enemy_pos in enemies:
        enemy_pos[1] += enemy_speed

    # 如果敌机超出屏幕，从列表中删除
    for enemy_pos in enemies:
        if enemy_pos[1] > 700:
            enemies.remove(enemy_pos)

    # 移动子弹
    if bullet_y > 0:
        bullet_y -= bullet_speed

    # 如果子弹和敌机相撞，敌机消失并生成爆炸特效
    for enemy_pos in enemies:
        if bullet_x + bullet.get_width() > enemy_pos[0] and bullet_x < enemy_pos[0] + enemy.get_width() and bullet_y < enemy_pos[1] + enemy.get_height() and bullet_y + bullet.get_height() > enemy_pos[1]:
            enemies.remove(enemy_pos)
            bullet_y = 0
            explosions.append([enemy_pos[0], enemy_pos[1], pygame.time.get_ticks()])

    # 清除之前的图像
    screen.fill((0, 0, 0))

    # 绘制背景
    screen.blit(background, (0, 0))

    # 绘制飞机
    screen.blit(player, (player_x, player_y))

    # 绘制敌机
    for enemy_pos in enemies:
        screen.blit(enemy, (enemy_pos[0], enemy_pos[1]))
    
    # 绘制子弹
    screen.blit(bullet, (bullet_x, bullet_y))
    # 检查子弹是否超出屏幕
    if bullet_y < 0:
        bullet_y = 0

    # 绘制爆炸特效
    for explosion_pos in explosions:
        # 爆炸特效显示0.s秒
        #if pygame.time.get_ticks() - explosion_pos[2] < 300:
        # 如果爆炸特效已经存在0.1秒以上，就从列表中删除
        if pygame.time.get_ticks() - explosion_pos[2] >= 100:
            explosions.remove(explosion_pos)
        else:
            screen.blit(explosion, (explosion_pos[0], explosion_pos[1]))

    # 更新屏幕
    pygame.display.update()

    # 如果飞机和敌机相撞，游戏结束
    for enemy_pos in enemies:
        if player_x + player.get_width() > enemy_pos[0] and player_x < enemy_pos[0] + enemy.get_width() and player_y < enemy_pos[1] + enemy.get_height() and player_y + player.get_height() > enemy_pos[1]:
            pygame.quit()
            exit()
